﻿//using PasteLoggerBoard.Localization;
using Microsoft.AspNetCore.Http;
using Volo.Abp.Application.Services;

namespace PasteLoggerBoard
{
    /// <summary>
    /// 
    /// </summary>
    public abstract class PasteLoggerBoardAppService : ApplicationService
    {
        /// <summary>
        /// 
        /// </summary>
        protected PasteLoggerBoardAppService()
        {
            //LocalizationResource = typeof(PasteLoggerBoardResource);
            ObjectMapperContext = typeof(PasteLoggerBoardApplicationModule);
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public abstract class IUserAppService : ApplicationService
    {
        /// <summary>
        /// 
        /// </summary>
        protected HttpContext _httpContext
        {
            get
            {
                if (private_httpcontext == null)
                {
                    var _ = LazyServiceProvider.LazyGetRequiredService<IHttpContextAccessor>();
                    private_httpcontext = _.HttpContext;
                }
                return private_httpcontext;
            }
        }

        private HttpContext private_httpcontext { get; set; } = null;


        /// <summary>
        /// 
        /// </summary>
        protected IUserAppService()
        {
            //_httpContext = accers.HttpContext;
        }

        /// <summary>
        /// 获取登录用户的ID
        /// </summary>
        /// <returns></returns>
        protected int ReadCurrentUserId()
        {
            if (_httpContext.Request.Headers.ContainsKey(PublicString.TokenHeadName))
            {
                var token = _httpContext.Request.Headers[PublicString.TokenHeadName].ToString();
                if (token.Contains('_'))
                {
                    //这里一般需要添加密钥校验
                    int.TryParse(token.Split('_')[1], out var userid);
                    return userid;
                }
            }
            return 0;
        }

        /// <summary>
        /// 获取当前用户的密钥
        /// </summary>
        /// <returns></returns>
        protected string ReadCurrentUserToken()
        {
            if (_httpContext.Request.Headers.ContainsKey(PublicString.TokenHeadName))
            {
                var token = _httpContext.Request.Headers[PublicString.TokenHeadName].ToString();
                return token;
            }
            return "";
        }
    }
}
